/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.matrimonyapp;

import com.matrimonybeans.CandidateProfileLocal;
import com.matrimonybeans.updateProfileLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ChauAnh
 */
public class updateProfile extends HttpServlet {

    @EJB
    private CandidateProfileLocal candidateProfile;
    @EJB
    private updateProfileLocal updateProfile;    

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String username = "";
            HttpSession ss = request.getSession();
            if (ss.getAttribute("login") == null) {
                out.println("<script type='text/javascript'>alert('Please login!');window.location.href = 'index.jsp';</script>");
            } else {
                username = ss.getAttribute("login").toString();
            }
            
            if (request.getParameter("txtEditFirstName") != null) {
                //out.println(request.getParameter("txtEditFirstName").toString()+" "+username);
                if(request.getParameter("txtEditFirstName").equals("")){
                    updateProfile.setFirstName(username, "no");
                }else{
                    updateProfile.setFirstName(username, request.getParameter("txtEditFirstName").toString());
                }                
            }

            if (request.getParameter("txtEditMiddleName") != null) {
                if(request.getParameter("txtEditMiddleName").equals("")){
                    updateProfile.setMiddleName(username, "no");
                }else{
                    updateProfile.setMiddleName(username, request.getParameter("txtEditMiddleName").toString());
                }
                
            }
            
            if (request.getParameter("txtEditLastName") != null) {
                if(request.getParameter("txtEditLastName").equals("")){
                    updateProfile.setLastName(username, "no");
                }else{
                    updateProfile.setLastName(username, request.getParameter("txtEditLastName").toString());
                }                
            }
            
            if (request.getParameter("cbEditGender") != null) {
                updateProfile.setGender(username, request.getParameter("cbEditGender").toString());
            }
            
            if (request.getParameter("cbEditDay") != null && request.getParameter("cbEditMonth") != null && request.getParameter("cbEditYears") != null) {
                if (!request.getParameter("cbEditDay").equals("Day")  && !request.getParameter("cbEditMonth").equals("Month") && !request.getParameter("cbEditYears").equals("Years")) {
                    String day = request.getParameter("cbEditDay").toString();
                    String month =request.getParameter("cbEditMonth").toString();
                    String years =request.getParameter("cbEditYears").toString();
                    updateProfile.setBirthdate(username, month+"/"+day+"/"+years);
                    //out.print("Date :"+month+"/"+"/"+day+"/"+years);
                }            
            }
            
            if (request.getParameter("cbEditMaritalStatus") != null) {
                updateProfile.setMaritalStatus(username, request.getParameter("cbEditMaritalStatus").toString());
            }
            
            
            HashMap listCandidateProfileDetails = candidateProfile.getCandidateProfile(username);
            ss.setAttribute("listCandidateProfileDetails", listCandidateProfileDetails);            
            response.sendRedirect("updateProfile.jsp");
        } catch (Exception ex) {
            response.sendRedirect("error.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
